using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class JobSeekers_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Contents["aduserid"] != null)
        {
            Response.Redirect("myaccount.aspx");
        }
        if (Request.Cookies["Remember"] != null)
        {
            HttpCookie httpCookie =
            Request.Cookies["UserNameAndTime"];
            txtUserName.Text = httpCookie.Values["UserName"].ToString();

        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string[] valueToGet = new string[3] { "", "", ""};
            RecruitVietnam cls = new RecruitVietnam();
            cls.ConnectionString = WebConfigurationManager.ConnectionStrings[
               "RecruitVietnam"].ConnectionString;
            valueToGet = cls.AdminLogin(txtUserName.Text, txtPassword.Text);
            if (valueToGet[0] == "OK")
            {
                Session.Add("aduserid", txtUserName.Text);                     
                Session.Add("adname", valueToGet[1]);                     
                WriteCookie(txtUserName.Text);
                Response.Redirect("myaccount.aspx");
                 
            }
            else
            {
                switch(valueToGet[0])
                {
                    case "U":
                        lblHeader.Text="User name is not existing,"+
                        " please re-enter your User name and try again.";
                        break;
                    case "P":
                        lblHeader.Text = "Invalid password, "+
                        "please re-enter your own password and try again.";
                        break;
                    default:
                        lblHeader.Text = "System error, " + cls.ErrorMessage;
                        break;
                }
            }
         }
         catch(Exception ex)
         {
            literalError.Text = ex.Message;
         }
    }
    void WriteCookie(string userName)
    {
        if (CheckBoxRememberUserName.Checked)
        {
            HttpCookie httpCookie = new HttpCookie("Remember", "Yes");
            Response.Cookies.Add(httpCookie);
            httpCookie = new HttpCookie("UserNameAndTime");
            httpCookie.Values.Add("UserName", userName);
            httpCookie.Values.Add("Time", DateTime.Now.ToShortDateString());
            Response.Cookies.Add(httpCookie);
        }
    }
}
